﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Blame.Decorator;
using Blame.Entity;
using Blame;

namespace BlameUI
{
	/// <summary>
	/// Interaction logic for Changeset.xaml
	/// </summary>
	public partial class ChangesetWindow : Window
	{
        //private DataTable _FileLogData;
	    private ObservableCollection<ChangesetPathAction> filelog;
        private User currentUser;
        private List<Changeset> logs;
	    private Changeset selectedChangeset;
	    private Controller _controller;

		public ChangesetWindow(User user, Controller controller)
		{
			this.InitializeComponent();
			
			currentUser = user;
            this._controller = controller;
			this.logs = _controller.GetUserLog(user);
			
			this.ShowInTaskbar = false;

		    setupUI();
		}

        private void setupUI()
        {
            this.Title = currentUser.Name + "'s Changesets";
            LblUser.Content = "User: " + currentUser.Name + " <" + currentUser.Email + ">";

            List<string> logList = new List<string>();

            foreach (Changeset c in logs)
            {
                logList.Add("Rev: " + c.RevisionNumber + " (" + c.Timestamp + ")");
            }

            this.ChangesetListBox.ItemsSource = logList;
        }

	    private void ChangesetListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            IEnumerator<Changeset> iter = logs.GetEnumerator();

            this.selectedChangeset = iter.Current;

            for (int i = 0; i <= ChangesetListBox.SelectedIndex; i++)
            {
                iter.MoveNext();
                selectedChangeset = iter.Current;
            }

	        this.LblRevision.Content = "Revision: " + selectedChangeset.RevisionNumber + " (" + selectedChangeset.Hash + ")";
            this.LblTime.Content = "Time: " + selectedChangeset.Timestamp;
            this.LblBranch.Content = "Branch: " + selectedChangeset.Branch;
	        String tags = "Tags: ";

            foreach(String tag in selectedChangeset.Tags)
            {
                tags = tags + " " + tag;
            }

	        this.TxtBoxTags.Text = tags;
	        this.TxtBoxCommitMessage.Text = "Message: " + selectedChangeset.CommitMessage;

            filelog = new ObservableCollection<ChangesetPathAction>();
            PopulateFileLogDataTable();
            dataGrid.UpdateLayout();
        }
		
#region FileLogDataTable
        /// <summary>
        /// Populates the Log DataTable with data from the log file
        /// </summary>
        private void PopulateFileLogDataTable()
        {
            foreach (ChangesetPathAction cpa in selectedChangeset.PathActions)
            {
                filelog.Add(cpa);
            }

            dataGrid.DataContext = filelog;
        }

        private void BtnDiff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
			if(this.selectedChangeset != null) {
                DiffWindow diff = new DiffWindow(_controller.GenerateDiff(selectedChangeset.RevisionNumber), selectedChangeset.RevisionNumber);
				diff.ShowDialog();
			}
        }

        #endregion
	}
}